<template>
  <div class="tableOut">
    <el-table ref="multipleTableRef" :data="list" stripe style="width: 600px" height="600px" class="tableData" :header-cell-style="{ background: '#F9F9F9', color: '#909399' }">
      <el-table-column prop="name" label="来源名称" width="250" />
      <el-table-column prop="state" label="状态" min-width="140">
        <template #default="scope">
          <div class="state_box">
            <el-switch v-model="scope.row.state" :active-value="2" :inactive-value="1" /><span style="margin: 0 10px">{{ scope.row.state == 1 ? '已关闭' : '已开启' }} </span>
            <div class="state" @click="onStatus(scope.row)"></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120">
        <template #default="scope">
          <el-button class="table_btn" size="small" @click="onDelDialog(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <div class="emty">
          <img class="emty_img" src="@/assets/noData.png" alt="" />
          <div class="emty_title">{{ list?.length || '暂无数据' }}</div>
        </div>
      </template>
    </el-table>
  </div>
  <el-button style="margin-top: 30px" type="primary" @click="onAddSource">新增来源</el-button>
  <!-- 弹窗 -->
  <div class="dialog_box">
    <el-dialog v-model="dialogFormVisible" title="新增来源" width="500px" @closed="onClosedForm">
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="来源名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入来源名称" maxlength="20" show-word-limit autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(formRef)">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <ZWDeletdialog ref="DeletdialogRef" @submit="onDelete" />
</template>
<script lang="ts" setup>
  import ZWDeletdialog from '@/components/ZWDeletdialog.vue'
  import { orderSeniorCreate, getOrderSeniorlist, orderUpdateStatus, orderUpdateDelete } from '@/api/salesleads/ordermanagement/orderlist'
  import { reactive, ref, onMounted } from 'vue'
  import type { FormInstance, FormRules } from 'element-plus'
  import { ElMessage } from 'element-plus'
  const formRef = ref<FormInstance>()
  const list = ref([])
  const dialogFormVisible = ref(false)
  const DeletdialogRef: any = ref(null)
  const rules = reactive<FormRules>({
    name: [{ required: true, message: '请输入来源名称', trigger: 'blur' }]
  })
  const form = reactive({
    name: ''
  })
  // 获取列表数据
  const getList = (val?: any) => {
    getOrderSeniorlist({ state: -1 }).then((res: any) => {
      list.value = res
      console.log(res)
    })
  }
  onMounted(() => {
    getList()
  })
  //新增原因
  const onAddSource = (data: any) => {
    dialogFormVisible.value = true
  }
  //保存
  const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
      if (valid) {
        console.log('submit!')
        orderSeniorCreate(form).then((res: any) => {
          getList()
          ElMessage.success('新增来源成功')
          dialogFormVisible.value = false
        })
      }
    })
  }
  // 状态
  const onStatus = (row: any) => {
    console.log(row)
    orderUpdateStatus({ id: row.id, state: row.state == 1 ? 2 : 1 })
      .then((res: any) => {
        getList()
        ElMessage.success(`${row.state == 1 ? '开启' : '关闭'}成功`)
      })
      .catch((err: any) => {
        console.log(err)
      })
  }
  // 删除
  const delId = ref('')
  const onDelDialog = (row: any) => {
    DeletdialogRef.value.appDialog.visible = true
    delId.value = row.id
  }
  const onDelete = () => {
    orderUpdateDelete({ id: delId.value }).then((res: any) => {
      getList()
      ElMessage.success('删除成功')
    })
  }
  const onClosedForm = () => {
    form.name = ''
  }
</script>

<style lang="scss" scoped>
  .tableOut {
    .state_box {
      position: relative;
      .state {
        width: 42px;
        height: 22px;
        position: absolute;
        top: 0;
        z-index: 3;
        cursor: pointer;
      }
    }
    .emty {
      margin-top: 100px;
      // display: flex;
      // flex-direction: column;
    }

    .emty_img {
      transform: translateX(15%);
    }

    .emty_title {
      margin-top: -50px;
    }
    .handle {
      display: flex;
      flex-wrap: wrap;
      font-size: 14px;

      .btn_item {
        padding: 0 8px;
        margin: 0 8px 12px 0px;
        text-align: center;
        background: #ffffff;
        border-radius: 2px;
        border: 1px solid #eaeaea;
        cursor: pointer;
      }
    }
  }
  .table_btn {
    margin-bottom: -8px;
    text-align: left;
    overflow: hidden;
    cursor: pointer;
    display: inline-block;
    vertical-align: middle;
    width: 80px;
    height: 24px;
    margin: 0 4px 8px;
    border: 1px solid #eaeaea;
    border-radius: 2px;
    text-align: center;
    background: #fff;
  }
  .dialog_box {
    :deep(.el-dialog) {
      .el-dialog__header {
        border-bottom: 1px solid #eaeaea;
      }
      .el-dialog__body {
        border-bottom: 1px solid #eaeaea;
      }
    }
  }
</style>
